<template>
    <div>
<h1>新增医生</h1>
<table class="table">
    <tbody>
        <tr>
            <td>医生姓名</td>
            <td>
                <input type="text" v-model="data.doctorName">
            </td>
        </tr>
        <tr>
            <td>头像</td>
            <td>
                <input type="file" @change="ImgUrls">
                <img :src="'https://localhost:7258/'+data.imgUrl" style="width: 60px;height: 60px;">
            </td>
        </tr>
        <tr>
            <td>所属科室</td>
            <td>
 <select v-model="data.kids">
                    <option value="0">请选择科室</option>
                    <option v-for="d in dataps" :value="d.kid">{{d.kName}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="新增" class="but btn-success" @click="Adddoctor">
            </td>
        </tr>
    </tbody>
</table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';
var router=useRouter();
onMounted(()=>{
Bind();
})

const data=ref({
      "did": 0,
  "doctorName": "",
  "imgUrl": "",
  "kids": 0,
  "createdate": "2025-05-19T03:22:48.096Z"
})

const Adddoctor=()=>{
    if(data.value.doctorName=="")
{
    alert("医生姓名不能为空");
    return;
}
    axios.post("/api/Doctor/AddDoctor",data.value).then(res=>{
        if(res.data>0)
    {
        alert("新增成功")
        router.push({path:"/Getdoctor"})
    }
    else
    {
        alert("新增失败")
    }
    })
}

//绑定下拉框
const dataps=ref([{
     "kid": 0,
    "kName": "",
}])

const Bind=()=>{
    axios.get("/api/Ks/GetDc",{params:dataps.value}).then(res=>{
        dataps.value=res.data;
    })
}


const ImgUrls=(e:any)=>{
var f=e.target.files[0];
var fd=new FormData();
fd.append("file",f);
if(f.size>2*1024*1024)
{
    alert("图片不能超过2MB")
    return;
}
axios.post("/api/Lode/UpFile",fd).then(res=>{
    data.value.imgUrl=res.data;
})
}
</script>

<style scoped>

</style>